import grails.util.GrailsUtil

import org.codehaus.groovy.grails.commons.ApplicationHolder

import auctionatr.Auction
import auctionatr.Bid
import auctionatr.Category
import auctionatr.User

class BootStrap {

	def getImage (filepath) {
		def path =  ApplicationHolder.application.parentContext.getResource("/auctionpictures/" + filepath).file
		File file = new File(path.absolutePath);
		byte[] b = new byte[(int) file.length()];
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			fileInputStream.read(b);
			return b
		} catch (FileNotFoundException e) {
			System.out.println("File Not Found.");
			e.printStackTrace();
		}
		catch (IOException e1) {
			System.out.println("Error Reading The File.");
			e1.printStackTrace();
		}
	}

	def init = { servletContext ->

		// nur füllen, wenn leer (production environment)
		if(GrailsUtil.environment == "development") {
			println "Beispielauktionen werden angelegt..."

			// Testnutzer mit Admin Rolle anlegen:
			final String BACKUP_ADMIN = 'admin'
			final String BACKUP_PSW = 'pass'
			def admin = null
			if (!User.findByUsername(BACKUP_ADMIN)) {
				admin = new User(username:BACKUP_ADMIN,password:BACKUP_PSW,email:'max.voekler@gmx.de', birthday:(new Date() - 365*30), facebookName: 'admin', aboutUser: 'here i am', twitterName: 'admin', firstName: 'ad', lastName: 'min', street: 'Rotebühlplatz 41', zip: 70174, city: 'Stuttgart', accountNumber: 343443, bankCode: '123456789',role:'admin',avatar: getImage("Smiley.gif"), avatarType: "image/gif")

				admin.save()
				if(admin.hasErrors()){
					println admin.errors
				}
			}

			// Testnutzer mit User Rolle anlegen:
			final String BACKUP_TESTUSER = 'testuser'
			if (!User.findByUsername(BACKUP_TESTUSER)) {
				def testuser = new User(username:BACKUP_TESTUSER,password:BACKUP_PSW,email:'test@user.de', birthday:(new Date() - 365*30), facebookName: 'admin', aboutUser: 'here i am', twitterName: 'admin', firstName: 'ad', lastName: 'min', street: 'here', zip: 8990, city: 'Stuttgart', accountNumber: 343443, bankCode: 'eee',role:'user',avatar: null, avatarType: null)

				testuser.save()
				if(testuser.hasErrors()){
					println testuser.errors
				}
			}
			// CAN mit User Rolle anlegen:
			final String BACKUP_CAN = 'can'
			def can =  null
			if (!User.findByUsername(BACKUP_CAN)) {
				can = new User(
						username:BACKUP_CAN,
						password:BACKUP_PSW,
						email:'canpaul91@googlemail.com',
						birthday:(new Date() - 365*30),
						facebookName: 'canpaul',
						aboutUser: 'ich komme aus berlin',
						twitterName: 'hab keins :(',
						firstName: 'Can Paul',
						lastName: 'Bineytioglu',
						street: 'Allmandring 99',
						zip: 70569,
						city: 'Stuttgart',
						accountNumber: 343443,
						bankCode: '2342242',
						role:'user',
						avatar: null,
						avatarType: null)

				can.save()
				if(can.hasErrors()){
					println can.errors
				}
			}

			// MAX mit User Rolle anlegen:
			final String BACKUP_MAX = 'max'
			def max = null
			if (!User.findByUsername(BACKUP_MAX)) {
				max = new User(
						username:BACKUP_MAX,
						password:BACKUP_PSW,
						email:'max.voekler@googlemail.com',
						birthday:(new Date() - 365*30),
						facebookName: 'admin',
						aboutUser: 'ich komme aus leipzip',
						twitterName: 'twitter find ich doof',
						firstName: 'Max',
						lastName: 'Voekler',
						street: 'Straussi 99',
						zip: 70569,
						city: 'Stuttgart',
						accountNumber: 343443,
						bankCode: '354645',
						role:'user',
						avatar: null,
						avatarType: null)

				max.save()
				if(max.hasErrors()){
					println max.errors
				}
			}

			// JOE mit User Rolle anlegen:
			final String BACKUP_JOE = 'joe'

			def joe = null
			if (!User.findByUsername(BACKUP_JOE)) {
				joe = new User(
						username:BACKUP_JOE,
						password:BACKUP_PSW,
						email:'joe@andre.de',
						birthday:(new Date() - 365*30),
						facebookName: 'masterOfJapan',
						aboutUser: 'japan for life ;)',
						twitterName: 'joeandre',
						firstName: 'Joe',
						lastName: 'Boden',
						street: 'Heilmannstr. 444',
						zip: 71234,
						city: 'Stuttgart',
						accountNumber: 343443,
						bankCode: '567867854',
						role:'user',
						avatar: null,
						avatarType: null)

				joe.save()
				if(joe.hasErrors()){
					println joe.errors
				}
			}

			// PHIL mit User Rolle anlegen:
			final String BACKUP_PH = 'phil'
			def phil = null
			if (!User.findByUsername(BACKUP_PH)) {
				phil = new User(
						username:BACKUP_PH,
						password:BACKUP_PSW,
						email:'phil@ip.de',
						birthday:(new Date() - 365*30),
						facebookName: 'philipus',
						aboutUser: 'sport ist mord, rauchen ist gold',
						twitterName: 'hab keins :(',
						firstName: 'Philip',
						lastName: 'Fitschen',
						street: 'Pfaffenwaldring 987',
						zip: 70569,
						city: 'Stuttgart',
						accountNumber: 343443,
						bankCode: '34563',
						role:'user',
						avatar: null,
						avatarType: null)

				phil.save()
				if(phil.hasErrors()){
					println phil.errors
				}
			}

			// Kategorie 'Computer und Software' anlegen
			def cat1 = new Category(
					name:"Computer und Software"
					)
			cat1.save()
			if(cat1.hasErrors()){
				println cat1.errors
			}

			// Kategorie 'Kleidung' anlegen
			def cat2 = new Category(
					name:"Kleidung und Accessoires"
					)
			cat2.save()
			if(cat2.hasErrors()){
				println cat2.errors
			}

			// Kategorie 'Haus und Garten' anlegen
			def cat3 = new Category(
					name:"Haus und Garten"
					)
			cat3.save()
			if(cat3.hasErrors()){
				println cat3.errors
			}

			// Kategorie 'Elektronik' anlegen
			def cat4 = new Category(
					name:"Elektronik"
					)
			cat4.save()
			if(cat4.hasErrors()){
				println cat4.errors
			}

			// Kategorie 'Antiquitäten und Kunst' anlegen
			def cat5 = new Category(
					name:"Antiquitäten und Kunst"
					)
			cat5.save()
			if(cat5.hasErrors()){
				println cat5.errors
			}

			// Kategorie 'Schmuck' anlegen
			def cat6 = new Category(
					name:"Schmuck"
					)
			cat6.save()
			if(cat6.hasErrors()){
				println cat6.errors
			}


			// Beispielauktion 1:
			def auction1 = new Auction(
					name:"iPhone 4",
					description:"Ich verkaufe ein nagelneues iPhone 4!",
					startPrice:1.0,
					finishDate:(new Date()+1),
					completed: false,
					categories:cat1,
					shipment:"UPS",
					state:"neu",
					users: max,
					picture: getImage("iphone.jpg"),
					pictureType: "image/jpg"
					)
			auction1.save()
			if(auction1.hasErrors()){
				println auction1.errors
			}

			// Beispielauktion 2:
			def auction2 = new Auction(
					name:"Galaxy",
					description:"gebraucht, kaum Kratzer",
					startPrice:1.5,
					finishDate:(new Date()+5),
					completed: false,
					categories:cat1,
					shipment:"UPS",
					state:"neu",
					users: can,
					picture: getImage("samsung-galaxy-s.jpg"),
					pictureType: "image/jpg"
					)
			auction2.save()
			if(auction2.hasErrors()){
				println auction2.errors
			}
			// Beispielauktion 3:
			def auction3 = new Auction(
					name:"Spülmaschine",
					description:"6000 U/min - sehr leistungsfähig",
					startPrice:1.0,
					finishDate:(new Date()+2),
					completed: false,
					categories:cat4,
					shipment:"UPS",
					state:"neu",
					users: phil,
					picture: getImage("spuelmaschine.jpg"),
					pictureType: "image/jpg"
					)
			auction3.save()
			if(auction3.hasErrors()){
				println auction3.errors
			}
			// Beispielauktion 4:
			def auction4 = new Auction(
					name:"Apple Mac",
					description:"der neue Mac, nur 1 Monate gebraucht",
					startPrice:1.5,
					finishDate:(new Date()+12),
					completed: false,
					categories:cat2,
					shipment:"UPS",
					state:"gebraucht",
					users: joe,
					picture: getImage("Apple-MacBook-Pro.jpg"),
					pictureType: "image/jpg"
					)
			auction4.save()
			if(auction4.hasErrors()){
				println auction4.errors
			}
			// Beispielauktion 5:
			def auction5 = new Auction(
					name:"Stuhl",
					description:"ich ziehe um, brauche Stuhl nicht mehr",
					startPrice:2.0,
					finishDate:(new Date()+5),
					completed: false,
					categories:cat3,
					shipment:"UPS",
					state:"neu",
					users: can,
					picture: getImage("stuhl.jpg"),
					pictureType: "image/jpg"
					)
			auction5.save()
			if(auction5.hasErrors()){
				println auction5.errors
			}

			// Beispielauktion 6:
			def auction6 = new Auction(
					name:"Wasserkocher",
					description:"kocht schon bei 80 Grad ;)",
					startPrice:1.0,
					finishDate:(new Date()+8),
					completed: false,
					categories:cat2,
					shipment:"UPS",
					state:"neu",
					users: max,
					picture: getImage("Wasserkocher.jpg"),
					pictureType: "image/jpg"
					)
			auction6.save()
			if(auction6.hasErrors()){
				println auction6.errors
			}

			// Beispielauktion 7:
			def auction7 = new Auction(
					name:"Buch Grails 1.2",
					description:"neuste Auflage, 2 Wochen benutzt",
					startPrice:2.0,
					finishDate:(new Date()+3),
					completed: false,
					categories:cat3,
					shipment:"DHL",
					state:"gebraucht",
					users: phil,
					picture: getImage("grails.jpg"),
					pictureType: "image/jpg"
					)
			auction7.save()
			if(auction7.hasErrors()){
				println auction7.errors
			}

			// Beispielauktion 8:
			def auction8 = new Auction(
					name:"Externe Festplatte 500 GB",
					description:"USB 3.0 - super schnell!!",
					startPrice:1.0,
					finishDate:(new Date()+8),
					completed: false,
					categories:cat1,
					shipment:"Hermes",
					state:"neu",
					users: joe,
					picture: getImage("externe_festplatte.jpg"),
					pictureType: "image/jpg"
					)
			auction8.save()
			if(auction8.hasErrors()){
				println auction8.errors
			}

			// Beispielauktion 9:
			def auction9 = new Auction(
					name:"Fernseher Toshiba",
					description:"leider ohne Fernbedienung, aber sonst einwandfrei :)",
					startPrice:3.33,
					finishDate:(new Date()+11),
					completed: false,
					categories:cat4,
					shipment:"Abholung",
					state:"gebraucht",
					users: can,
					picture: getImage("Toshiba-fernseher.jpg"),
					pictureType: "image/jpg"
					)
			auction9.save()
			if(auction9.hasErrors()){
				println auction9.errors
			}

			// Beispielauktion 10:
			def auction10 = new Auction(
					name:"Logitech WebCam",
					description:"mit Spezialeffekten! Zwar gebraucht, aber Zustand wie neu!",
					startPrice:1.0,
					finishDate:(new Date()+4),
					completed: false,
					categories:cat4,
					shipment:"Abholung",
					state:"wie neu",
					users: max,
					picture: getImage("logitech_webcam.jpg"),
					pictureType: "image/jpg"
					)
			auction10.save()
			if(auction10.hasErrors()){
				println auction10.errors
			}

			// Beispielauktion 11:
			def auction11 = new Auction(
					name:"Bärtchen",
					description:"Hat schon vieles erlebt!",
					startPrice:100.0,
					finishDate:(new Date()+4),
					completed: false,
					categories:cat2,
					shipment:"Abholung",
					state:"wie neu",
					users: phil,
					picture: getImage("bart.png"),
					pictureType: "image/png"
					)
			auction11.save()
			if(auction11.hasErrors()){
				println auction11.errors
			}

			// Beispielauktion 12:
			def auction12 = new Auction(
					name:"Kopfhörer",
					description:"Top-Qualität und so gut wie neu! Habe neue geschenkt bekommen, deshalb verkaufe ich.",
					startPrice:12.0,
					finishDate:(new Date()+4),
					completed: false,
					categories:cat4,
					shipment:"Hermes",
					state:"wie neu",
					users: joe,
					picture: getImage("kopfhoerer.jpg"),
					pictureType: "image/jpg"
					)
			auction12.save()
			if(auction12.hasErrors()){
				println auction12.errors
			}

			// Beispielauktion 13:
			def auction13 = new Auction(
					name:"Rolex",
					description:"Ich verkaufe hier eine wunderschöne goldene Luxus-Rolex!",
					startPrice:45.0,
					finishDate:(new Date()+2),
					completed: false,
					categories:cat6,
					shipment:"Abholung",
					state:"gebraucht",
					users: can,
					picture: getImage("rolex.jpg"),
					pictureType: "image/jpg"
					)
			auction13.save()
			if(auction13.hasErrors()){
				println auction13.errors
			}

			// Beispielauktion 14:
			def auction14 = new Auction(
					name:"Adidas Handschuhe",
					description:"Passen mir leider nicht.",
					startPrice:1.0,
					finishDate:(new Date()+1),
					completed: false,
					categories:cat2,
					shipment:"UPS",
					state:"wie neu",
					users: max,
					picture: getImage("adidas-handschuhe.jpg"),
					pictureType: "image/jpg"
					)
			auction14.save()
			if(auction14.hasErrors()){
				println auction14.errors
			}

			// Beispielauktion 15:
			def auction15 = new Auction(
					name:"Perlenkette",
					description:"Mit Glasperlen, sehr geschmacksvoll :)",
					startPrice:2.8,
					finishDate:(new Date()+2),
					completed: false,
					categories:cat6,
					shipment:"Hermes",
					state:"neu",
					users: joe,
					picture: getImage("perlenkette.jpg"),
					pictureType: "image/jpg"
					)
			auction15.save()
			if(auction15.hasErrors()){
				println auction15.errors
			}

			// Beispielauktion 16:
			def auction16 = new Auction(
					name:"Gemälde von Van Gogh",
					description:"Gemälde von Vincent van Gogh 'Sonnenblumen'",
					startPrice:27.0,
					finishDate:(new Date()+5),
					completed: false,
					categories:cat5,
					shipment:"Abholung",
					state:"gebraucht",
					users: max,
					picture: getImage("VanGogh.jpg"),
					pictureType: "image/jpg"
					)
			auction16.save()
			if(auction16.hasErrors()){
				println auction16.errors
			}

			// Beispielgebot 1:
			def bid1 = new Bid(
					gebot:3.0,
					users: can,	// != admin
					auctions: auction1 // iphone4 - startPrice:1.0
					)
			bid1.save()
			if(bid1.hasErrors()){
				println bid1.errors
			}
			// Beispielgebot 2:
			def bid2 = new Bid(
					gebot:5.0,		// > 1.0
					users: phil,	// != can
					auctions: auction3 // spülmaschine - startPrice:1.0
					)
			bid2.save()
			if(bid2.hasErrors()){
				println bid2.errors
			}
			// Beispielgebot 3:
			def bid3 = new Bid(
					gebot:6.0,	// > bid2.gebot also >5.0
					users: max,	// != can
					auctions: auction3 // spülmaschine - startPrice:1.0
					)
			bid3.save()
			if(bid3.hasErrors()){
				println bid3.errors
			}

			// Beispielgebot 4:
			def bid4 = new Bid(
					gebot:11.0,
					users: phil,	// != can
					auctions: auction3 // spülmaschine - startPrice:1.0
					)
			bid4.save()
			if(bid4.hasErrors()){
				println bid4.errors
			}
			// Beispielgebot 5:
			def bid5 = new Bid(
					gebot:120.0,
					users: can,	// != max
					auctions: auction4 // Mac - startPrice:1.5
					)
			bid5.save()
			if(bid5.hasErrors()){
				println bid5.errors
			}

			// Beispielgebot 6:
			def bid6 = new Bid(
					gebot:55.0,
					users: can,
					auctions: auction13
					)
			bid6.save()
			if(bid6.hasErrors()){
				println bid6.errors
			}

			// Beispielgebot 7:
			def bid7 = new Bid(
					gebot:104.0,
					users: max,
					auctions: auction11
					)
			bid7.save()
			if(bid7.hasErrors()){
				println bid7.errors
			}

			// Beispielgebot 8:
			def bid8 = new Bid(
					gebot:55.0,
					users: max,
					auctions: auction12
					)
			bid8.save()
			if(bid8.hasErrors()){
				println bid8.errors
			}

			// Beispielgebot 9:
			def bid9 = new Bid(
					gebot:4.0,
					users: can,
					auctions: auction6
					)
			bid9.save()
			if(bid9.hasErrors()){
				println bid9.errors
			}

			// Beispielgebot 10:
			def bid10 = new Bid(
					gebot:12.2,
					users: joe,
					auctions: auction3
					)
			bid10.save()
			if(bid10.hasErrors()){
				println bid10.errors
			}

			// Beispielgebot11:
			def bid11 = new Bid(
					gebot:12.7,
					users: max,
					auctions: auction3
					)
			bid11.save()
			if(bid11.hasErrors()){
				println bid11.errors
			}

			// Beispielgebot12:
			def bid12 = new Bid(
					gebot:15.5,
					users: phil,
					auctions: auction3
					)
			bid12.save()
			if(bid12.hasErrors()){
				println bid12.errors
			}

			// Beispielgebot13:
			def bid13 = new Bid(
					gebot:3.5,
					users: can,
					auctions: auction15
					)
			bid13.save()
			if(bid13.hasErrors()){
				println bid13.errors
			}

			// Beispielgebot14:
			def bid14 = new Bid(
					gebot:4.5,
					users: phil,
					auctions: auction15
					)
			bid14.save()
			if(bid14.hasErrors()){
				println bid14.errors
			}

			// Beispielgebot15:
			def bid15 = new Bid(
					gebot:7.1,
					users: joe,
					auctions: auction15
					)
			bid15.save()
			if(bid15.hasErrors()){
				println bid15.errors
			}

		}

	}

	def destroy = {
	}
}

